package com.kingcore.director.setting.ui;

import java.awt.Color;
import java.awt.Event;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;

import wzw.beans.DbBean;
import wzw.sql.DatabaseOperator;
import wzw.util.ComponentUtil;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.ui.DirectorDialog;
import com.kingcore.director.core.util.DirectorFileUtils;
import com.kingcore.director.main.MainFrame;
import com.kingcore.director.setting.bean.FilesCopyBean;
import com.kingcore.framework.context.ApplicationContext;
import com.sunking.swing.JDirChooser;
import com.sunking.swing.JFileTree;
import com.sunking.swing.JFileTree.AllFileFilter;
import com.sunking.swing.JTreeComboBox;

public class FileCopyDialog extends DirectorDialog
	implements DatabaseOperator,
				ItemListener,
				ActionListener,
				MouseListener {

	private JPanel mainPanel = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JComboBox lbbh = null;
	private JComboBox cptype = null;
	private JTextField destPath = null;
	private JButton btnCopy = null;
	private JScrollPane jScrollPane = null;
	private JTable table = null;
	private JButton btnAdd = null;
	private JButton btnDel = null;
	private JComboBox path = null;

	Logger log = Logger.getLogger(FileCopyDialog.class);
	private JLabel jLabel3 = null;
	private JButton btnSaveLb = null;
	private JButton btnChoose = null;
	private JPopupMenu popupMenuForTable = null;	//JTable 的 JPopupMenu
	private JMenuItem jpmi_Table_Useful;
	private JMenuItem jpmi_Table_Delete;
	private JMenuItem jpmi_Table_Execute;
	private JMenuItem jpmi_Table_Execute_FX;	//反向复制
	private JMenuItem jpmi_Table_Execute_Del_FX;	//反向删除
	private JMenuItem jpmi_Table_ExecuteAll;
	private JMenuItem jpmi_Table_ExecuteAll_FX;	//反向复制
	private JMenuItem jpmi_Table_Execute_Path_Copy;	//复制路径
	private JMenuItem jpmi_Table_CreatePath;  //创建文件夹
	private JMenuItem jpmi_Table_Open;  //open file or path
	private JButton btnCopyRev = null;
	
	/**
	 * This method initializes lbbh	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getLbbh() {
		if (lbbh == null) {
			lbbh = new JComboBox();
			lbbh.setBounds(new java.awt.Rectangle(90,9,176,23));
			//lbbh.addContainerListener( this );
			lbbh.addItemListener( this );
		}
		return lbbh;
	}

	/**
	 * This method initializes cptype	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCptype() {
		if (cptype == null) {
			cptype = new JComboBox();
			cptype.setBounds(new java.awt.Rectangle(365,9,117,23));
			ComponentUtil.initComboBox( cptype,new String[][]{{"1","硬盘到硬盘"},{"2","硬盘到移动盘"},{"3","移动盘到硬盘"}} );
			cptype.setSelectedIndex(2) ;
		}
		return cptype;
	}

	/**
	 * This method initializes destPath	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JTextField getDestPath() {
		if (destPath == null) {
			destPath = new JTextField();	//JComboBox
			destPath.setBounds(new java.awt.Rectangle(90,43,107,23));
			//((JTreeComboBox)destPath).setTree(new JFileTree());
			destPath.setEditable(false);
		}
		return destPath;
	}

	/**
	 * This method initializes btnCopy	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCopy() {
		if (btnCopy == null) {
			btnCopy = new JButton();
			btnCopy.setBounds(new java.awt.Rectangle(324,40,64,27));
			btnCopy.setText("复制");
			btnCopy.addActionListener( this );
		}
		return btnCopy;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new java.awt.Rectangle(12,105,470,338));
			jScrollPane.setViewportView(getTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes table	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.setModel( new DefaultTableModel(1,2));
			table.setEnabled(true);
			table.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); //SINGLE_SELECTION
			
			table.setShowGrid(true);
			//tztj.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			table.getTableHeader().setReorderingAllowed(false);
			table.setForeground(Color.blue);
			((DefaultTableModel)table.getModel()).setColumnIdentifiers( new String[]{"启用否","文件路径"});
			//table.getColumn("是否启用").setWidth(50);
			//table.getModel().getColumnName(0).
			//table.getColumnModel().getColumn(0).setWidth( 10 );
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

			//table.getColumn("是否启用").setPreferredWidth( 50 );
			table.getColumnModel().getColumn(0).setPreferredWidth( 45 );
			table.getColumnModel().getColumn(1).setPreferredWidth( 405 );
			//table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
			table.addMouseListener( this );
		}
		return table;
	}

	/**
	 * This method initializes btnAdd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton();
			btnAdd.setBounds(new java.awt.Rectangle(297,75,82,27));
			btnAdd.setText("增加");
			btnAdd.addActionListener( this );
		}
		return btnAdd;
	}

	/**
	 * This method initializes btnDel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton();
			btnDel.setBounds(new java.awt.Rectangle(395,75,79,27));
			btnDel.setText("删除");
			btnDel.addActionListener( this );
		}
		return btnDel;
	}

	/**
	 * This method initializes path	,
	 * Zeven on 2007-03-21, add AllFileFilter not only Directory.
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getPath() {
		if (path == null) {
			path = new JTreeComboBox();	//JComboBox();
			path.setBounds(new java.awt.Rectangle(90,76,185,23));
			((JTreeComboBox)path).setTree(new JFileTree( new AllFileFilter()));
		}
		return path;
	}

	/**
	 * This method initializes btnSaveLb	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnSaveLb() {
		if (btnSaveLb == null) {
			btnSaveLb = new JButton();
			btnSaveLb.setBounds(new java.awt.Rectangle(231,40,89,27));
			btnSaveLb.setText("保存设置");
			btnSaveLb.addActionListener( this );
		}
		return btnSaveLb;
	}

	/**
	 * This method initializes btnChoose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnChoose() {
		if (btnChoose == null) {
			btnChoose = new JButton();
			btnChoose.setBounds(new java.awt.Rectangle(199,42,27,24));
			btnChoose.setText("...");
			btnChoose.addActionListener( this );
		}
		return btnChoose;
	}

	/**
	 * This method initializes btnCopyRev	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnCopyRev() {
		if (btnCopyRev == null) {
			btnCopyRev = new JButton();
			btnCopyRev.setBounds(new java.awt.Rectangle(395,40,89,27));
			btnCopyRev.setText("反向复制");
			btnCopyRev.addActionListener( this );
		}
		return btnCopyRev;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public FileCopyDialog() {
		this(null);
	}

	public FileCopyDialog(MainFrame frame) {
		super(frame, true);
		initialize();
		
		try {
			fetchData();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(499, 479);
		this.setContentPane(getMainPanel());
		this.setResizable( false );
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getMainPanel() {
		if (mainPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new java.awt.Rectangle(28,76,74,23));
			jLabel3.setText("文件夹：");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new java.awt.Rectangle(276,9,80,23));
			jLabel2.setText("复制类型：");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new java.awt.Rectangle(28,43,90,23));
			jLabel1.setText("复制目标：");
			jLabel = new JLabel();
			jLabel.setBounds(new java.awt.Rectangle(28,9,90,23));
			jLabel.setText("复制类别：");
			mainPanel = new JPanel();
			mainPanel.setLayout(null);
			mainPanel.add(jLabel, null);
			mainPanel.add(jLabel1, null);
			mainPanel.add(jLabel2, null);
			mainPanel.add(getLbbh(), null);
			mainPanel.add(getCptype(), null);
			mainPanel.add(getDestPath(), null);
			mainPanel.add(getBtnCopy(), null);
			mainPanel.add(getJScrollPane(), null);
			mainPanel.add(getBtnAdd(), null);
			mainPanel.add(getBtnDel(), null);
			mainPanel.add(getPath(), null);
			mainPanel.add(jLabel3, null);
			mainPanel.add(getBtnSaveLb(), null);
			mainPanel.add(getBtnChoose(), null);
			mainPanel.add(getBtnCopyRev(), null);
		}
		return mainPanel;
	}


	/**
	 * Table的弹出式菜单
	 */
	private JPopupMenu getPopupMenuForTable(){
		
		if(popupMenuForTable == null){
			popupMenuForTable = new JPopupMenu();
			
			jpmi_Table_Useful = new JMenuItem();
			jpmi_Table_Useful.setText("设置为无效");
			jpmi_Table_Useful.addActionListener( this );
			jpmi_Table_Useful.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_M,Event.CTRL_MASK) );
			
			jpmi_Table_Delete = new JMenuItem();
			jpmi_Table_Delete.setText("删除");
			jpmi_Table_Delete.addActionListener( this );
			jpmi_Table_Delete.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_D,Event.CTRL_MASK) );
			
			jpmi_Table_Execute = new JMenuItem();
			jpmi_Table_Execute.setText("执行选中行复制");
			jpmi_Table_Execute.addActionListener( this );
			jpmi_Table_Execute.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK) );
			
			jpmi_Table_Execute_FX = new JMenuItem();
			jpmi_Table_Execute_FX.setText("执行选中行的反向复制");
			jpmi_Table_Execute_FX.addActionListener( this );
			jpmi_Table_Execute_FX.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_R,Event.CTRL_MASK) );

			jpmi_Table_Execute_Del_FX = new JMenuItem();
			jpmi_Table_Execute_Del_FX.setText("执行选中行的备份信息删除");
			jpmi_Table_Execute_Del_FX.addActionListener( this );
			jpmi_Table_Execute_Del_FX.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.CTRL_MASK) );
			
			jpmi_Table_ExecuteAll = new JMenuItem();
			jpmi_Table_ExecuteAll.setText("执行所有行的复制");
			jpmi_Table_ExecuteAll.addActionListener( this );
			//jpmi_Table_ExecuteAll.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK) );
			
			jpmi_Table_ExecuteAll_FX = new JMenuItem();
			jpmi_Table_ExecuteAll_FX.setText("执行所有行的反向复制"); 
			jpmi_Table_ExecuteAll_FX.addActionListener( this );
			//jpmi_Table_ExecuteAll_FX.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_R,Event.CTRL_MASK) );
			
			jpmi_Table_CreatePath = new JMenuItem();
			jpmi_Table_CreatePath.setText("创建选中行的文件夹");
			jpmi_Table_CreatePath.addActionListener( this );
			//jpmi_Table_CreatePath.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK) );
			
			jpmi_Table_Execute_Path_Copy = new JMenuItem();
			jpmi_Table_Execute_Path_Copy.setText("Copy full path"); //复制文件全路径
			jpmi_Table_Execute_Path_Copy.addActionListener( this );

			jpmi_Table_Open = new JMenuItem();
			jpmi_Table_Open.setText("Open");
			jpmi_Table_Open.addActionListener( this );

			
			popupMenuForTable.add( jpmi_Table_Useful );
			popupMenuForTable.add( jpmi_Table_Delete );
			popupMenuForTable.addSeparator();

			popupMenuForTable.add( jpmi_Table_Execute );
			popupMenuForTable.add( jpmi_Table_Execute_FX );
			popupMenuForTable.addSeparator();
			
			popupMenuForTable.add( jpmi_Table_Execute_Del_FX );
			popupMenuForTable.addSeparator();
			
			popupMenuForTable.add( jpmi_Table_ExecuteAll );
			popupMenuForTable.add( jpmi_Table_ExecuteAll_FX );
			
			popupMenuForTable.addSeparator();
			popupMenuForTable.add( jpmi_Table_CreatePath );
			popupMenuForTable.add( jpmi_Table_Execute_Path_Copy );
			popupMenuForTable.add( jpmi_Table_Open );
			
		}
		
		return popupMenuForTable;
			
		
	}
	
	/**
	 * implements DatabaseOperator
	 */
	public boolean fetchData() throws SQLException {
		ComponentUtil.initComboBox( lbbh, "Select Val,Txt,isDefault From "+DirectorConstants.SYS_Class+" Where LBBH='000001' Order By Orderby ASC");

		String ts_lbbh = ComponentUtil.getSelectedItemCode( lbbh );
		getDataByLbbh(ts_lbbh);
		
		return true;
	}

	public boolean saveData() throws SQLException {
		// TODO Auto-generated method stub
		return true;
	}

	/**
	 * implements ItemListener
	 * @param e
	 */
	public void itemStateChanged(ItemEvent e) {
		Object src = e.getSource();
		if(src==lbbh){
			String ts_lbbh = ComponentUtil.getSelectedItemCode( lbbh );
			getDataByLbbh(ts_lbbh);
		}
		
	}

	
	/**
	 * 由两者以上调用该方法。
	 * @param ts_lbbh
	 */
	private void getDataByLbbh(String ts_lbbh){

		String sql="Select Useful,Path From "+DirectorConstants.FMT_FileCopy+" Where LBBH='"+ts_lbbh+"' Order By Path ASC";
		log.info( sql );
		ComponentUtil.initTable( table, sql);
		
		sql="Select CPTYPE,DestPath From "+DirectorConstants.FMT_FileCopy_Class+" Where LBBH='"+ts_lbbh+"'";
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;

		String ts_cptype;
		String ts_destPath ;
 
		try {
			conn= ApplicationContext.getInstance().getDataSourceManager().getConnection();
			stmt=conn.createStatement();
			log.info( sql );
			rs=stmt.executeQuery(sql);
			if(rs.next()){

				ts_cptype = rs.getString("CPTYPE");
				ts_destPath =rs.getString("DestPath");
				//System.out.println(" ts_cptype="+ts_cptype +" ts_destPath="+ts_destPath);
				ComponentUtil.setSelectedItemByCode( cptype,  ts_cptype);
				destPath.setText(ts_destPath );	//rs.getString("DestPath")
//				//destPath.setSelectedItem( rs.getString("DestPath") );
//				try {
//					//((JFileTree)((JTreeComboBox)getDestPath()).getTree()).setSelectFile( new File(ts_destPath) ) ;
//				} catch (Exception e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
			}

		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this,e1.getMessage(),"查询失败",JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();						
		} finally{
			//释放资源
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
				if(conn!=null){
					conn.close();
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * implements ActionListener
	 */
	public void actionPerformed(ActionEvent e) {
		Object src = e.getSource();

		// 添加
		if(src==btnAdd){
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_path = "";

			if( ts_lbbh==null || ts_lbbh.trim().equals("")){
				JOptionPane.showMessageDialog(this,"没有类别编号！");
				lbbh.requestFocus();
				return;
			}
			if( getPath().getSelectedItem()==null ){
				JOptionPane.showMessageDialog(this,"请选择路径！");
				path.requestFocus();
				return;
			} else {
				//ts_path = getPath().getSelectedItem().toString();
				ts_path = ((JFileTree)((JTreeComboBox)getPath()).getTree()).getSelectFile().getPath() ;
			}

			String sql="Insert Into "+DirectorConstants.FMT_FileCopy+"(LBBH,USEFUL,PATH) " +
					"Values('"+ts_lbbh+"','Y','"+ 
							   ts_path+"')";
			log.info( sql );
			DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
			try {
				dbBean.executeUpdate( sql );
				Vector v_row = new Vector();
				v_row.add("Y");
				v_row.add(ts_path);
				((DefaultTableModel)table.getModel()).addRow(v_row);
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
			

		// 删除
		} else if( src==btnDel ||
				   src==jpmi_Table_Delete ){
			
			int row = getTable().getSelectedRow();
			if(row<0){
				JOptionPane.showMessageDialog(this,"请选择要删除的行！");
				return ;
			}
			if(JOptionPane.showConfirmDialog(this,"确认删除当前信息吗？","提示",JOptionPane.OK_CANCEL_OPTION)
					!=JOptionPane.OK_OPTION)
				return;
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_path  = getTable().getValueAt( row,1).toString();

			String sql="Delete From "+DirectorConstants.FMT_FileCopy +
					" Where LBBH='"+ts_lbbh+"' "+ 
					  " And PATH ='"+ts_path+"'";
			DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
			try {
				log.debug( sql );
				dbBean.executeUpdate( sql );
				((DefaultTableModel)table.getModel()).removeRow( row );
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
			
		} else if ( src==btnCopy ||
			        src==jpmi_Table_ExecuteAll ){

			 if(JOptionPane.showConfirmDialog(this,"确认复制所有文件，并且确认当前系统时间准确吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;
			// 复制文件
			 // FilesCopyBean.setFilePath( System.getProperty(MainFrame.USER_DATA_PATH));
			 try {
				 String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
				 String ts_cptype= ComponentUtil.getSelectedItemCode( getCptype() );
				// String ts_destPath= ((JFileTree)((JTreeComboBox)getDestPath()).getTree()).getSelectFile().getPath() ;
				 String ts_destPath= getDestPath().getText();
				 //System.out.println("dp=" +ts_destPath);
				 FilesCopyBean.main(new String[]{ts_lbbh,ts_cptype,ts_destPath, "SX", null});
				 JOptionPane.showMessageDialog(this, "文件复制成功！" );
			
			 } catch (Exception e1) {
				JOptionPane.showMessageDialog(this, e1.getMessage() );
				return;
			 }
		
		} else if ( src==btnCopyRev ||
			        src==jpmi_Table_ExecuteAll_FX ){	//反向复制

			 if(JOptionPane.showConfirmDialog(this,"确认反向复制所有文件，并且确认当前系统时间准确吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;
			// 复制文件
			 // FilesCopyBean.setFilePath( System.getProperty(MainFrame.USER_DATA_PATH));
			 try {
				 String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
				 String ts_cptype= ComponentUtil.getSelectedItemCode( getCptype() );
				// String ts_destPath= ((JFileTree)((JTreeComboBox)getDestPath()).getTree()).getSelectFile().getPath() ;
				 String ts_destPath= getDestPath().getText();
				 //System.out.println("dp=" +ts_destPath);
				 FilesCopyBean.main(new String[]{ts_lbbh,ts_cptype,ts_destPath, "FX", null});
				 JOptionPane.showMessageDialog(this, "文件复制成功！" );
			
			 } catch (Exception e1) {
				JOptionPane.showMessageDialog(this, e1.getMessage() );
				return;
			 }
		
		} else if(src==btnSaveLb){
			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_cptype = ComponentUtil.getSelectedItemCode( getCptype() );
			//String ts_destPath = ((JFileTree)((JTreeComboBox)getDestPath()).getTree()).getSelectFile().getPath() ;
			String ts_destPath = getDestPath().getText() ;

			if( ts_lbbh==null || ts_lbbh.trim().equals("")){
				JOptionPane.showMessageDialog(this,"没有类别编号！");
				lbbh.requestFocus();
				return;
			}
			if( ts_cptype==null || ts_cptype.trim().equals("") ){
				JOptionPane.showMessageDialog(this,"请复制的类别！");
				cptype.requestFocus();
				return;
			}
			
			if( ts_destPath==null ){
				ts_destPath = "";
			}


			// 先删除，后插入
			String sql;				
			sql="Delete From "+DirectorConstants.FMT_FileCopy_Class+" Where LBBH='"+ts_lbbh+"'";
			log.info( sql );
			DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
			try {
				dbBean.executeUpdate( sql );
			} catch (SQLException e1) {
			}
			
			
			sql="Insert Into "+DirectorConstants.FMT_FileCopy_Class+"(LBBH,DESTPATH,CPTYPE) " +
			"Values('"+ts_lbbh+"','"+ 
					   ts_destPath+"','"+ 
					   ts_cptype+"')";
			log.info( sql );
			try {
				dbBean.executeUpdate( sql );
				JOptionPane.showMessageDialog(this,"设置保存成功！");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
			
		} else if (src==btnChoose ){
	        File f = JDirChooser.showDialog( getLbbh().getParent(), "Please Select directory", true, null,
            "Please Select");
			if(f != null){
				String ts_path = f.getAbsolutePath();
				//ts_path = ts_path.replace("\\","/"); //by wzw:keep as same as File.separator
				if( !ts_path.endsWith(File.separator)){
					ts_path += File.separator;
				}
				getDestPath().setText(  ts_path );
			}			

		} else if (src==jpmi_Table_Useful ){

			int ti_row = getTable().getSelectedRow();
			if(ti_row<0) return;

			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_useful = getTable().getValueAt( ti_row,0).toString();
			String ts_path = getTable().getValueAt( ti_row,1).toString();
			ts_useful = (ts_useful.equals("Y"))?"N":"Y";
			String ts_sql="Update "+DirectorConstants.FMT_FileCopy+" Set USEFUL='"+ts_useful+"' WHERE LBBH='"+ts_lbbh+"' AND PATH='"+ts_path+"'";
			log.info( ts_sql );
			try {
				DbBean dbBean = new DbBean(DbBean.DefaultPoolName);
				dbBean.executeUpdate( ts_sql );
				getTable().setValueAt( ts_useful, ti_row, 0);
				JOptionPane.showMessageDialog(this,"设置保存成功！");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this,e1.getMessage(),"数据库操作失败",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
		} else if (src==jpmi_Table_Execute ){

			int[] ti_rows = getTable().getSelectedRows();
			if (ti_rows.length<1) {
				JOptionPane.showMessageDialog(this,"请选择要执行复制的行！");
				return ;
			}
			 if(JOptionPane.showConfirmDialog(this,"确认执行选中的 "+ti_rows.length+"行的复制，并且确认当前系统时间准确吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;

			String ts_paths = "";			
			for (int ti_row : ti_rows) {
				if(ti_row<0) break;
				ts_paths +="'"+ getTable().getValueAt( ti_row,1).toString() +"',";
			}
			ts_paths = ts_paths.substring(0, ts_paths.length()-1 );

			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			//String ts_useful = getTable().getValueAt( ti_row,0).toString();

			String ts_cptype= ComponentUtil.getSelectedItemCode( getCptype() );
			String ts_destPath= getDestPath().getText();
			// 复制文件
			 // FilesCopyBean.setFilePath( System.getProperty(MainFrame.USER_DATA_PATH));
			 try {
				// String ts_destPath= ((JFileTree)((JTreeComboBox)getDestPath()).getTree()).getSelectFile().getPath() ;
				 //System.out.println("dp=" +ts_destPath);
				 FilesCopyBean.main(new String[]{ts_lbbh,ts_cptype,ts_destPath, "SX", ts_paths });
				 JOptionPane.showMessageDialog(this, "文件复制成功！" );
			
			 } catch (Exception e1) {
				JOptionPane.showMessageDialog(this, "文件复制完成，但是有错误：" + e1.getMessage() );
				return;
			 }


		} else if (src==jpmi_Table_Execute_FX ){

			int[] ti_rows = getTable().getSelectedRows();
			if (ti_rows.length<1) {
				JOptionPane.showMessageDialog(this,"请选择要反向复制的行！");
				return ;
			}
			 if(JOptionPane.showConfirmDialog(this,"确认执行选中的 "+ti_rows.length+"行的反向复制，并且确认当前系统时间准确吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;

			String ts_paths = "";			
			for (int ti_row : ti_rows) {
				if(ti_row<0) break;
				ts_paths +="'"+ getTable().getValueAt( ti_row,1).toString() +"',";
			}
			ts_paths = ts_paths.substring(0, ts_paths.length()-1 );

			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			//String ts_useful = getTable().getValueAt( ti_row,0).toString();
			//String ts_path = getTable().getValueAt( ti_row,1).toString();

			String ts_cptype= ComponentUtil.getSelectedItemCode( getCptype() );
			String ts_destPath= getDestPath().getText();
			// 复制文件
			 // FilesCopyBean.setFilePath( System.getProperty(MainFrame.USER_DATA_PATH));
			 try {
				// String ts_destPath= ((JFileTree)((JTreeComboBox)getDestPath()).getTree()).getSelectFile().getPath() ;
				 //System.out.println("dp=" +ts_destPath);
				 FilesCopyBean.main(new String[]{ts_lbbh,ts_cptype,ts_destPath, "FX", ts_paths });
				 JOptionPane.showMessageDialog(this, "文件反向复制成功！" );
			
			 } catch (Exception e1) {
				JOptionPane.showMessageDialog(this, e1.getMessage() );
				return;
			 }


		} else if (src==jpmi_Table_Execute_Del_FX ){	// 删除选中行的备份信息

			int[] ti_rows = getTable().getSelectedRows();
			if (ti_rows.length<1) {
				JOptionPane.showMessageDialog(this,"请选择要删除的行！");
				return ;
			}
			 if(JOptionPane.showConfirmDialog(this,"确认执行选中的 "+ti_rows.length+"行的备份信息删除吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;

			String ts_lbbh = ComponentUtil.getSelectedItemCode( getLbbh() );
			String ts_cptype= ComponentUtil.getSelectedItemCode( getCptype() );
			String ts_destPath= getDestPath().getText();
			// 删除文件
			String toFolder  =ts_destPath;  //.replace("\\","/"); //by wzw:keep as same as File.separator
			String thisCopyTo = null;
			String thisPath = null;
			File tmp_file = null;
			int beginPos = 0;
			String str_err = "";
			for (int ti_row : ti_rows) {
				if(ti_row<0) break;
				thisPath = getTable().getValueAt( ti_row,1).toString();
				thisCopyTo = toFolder + thisPath.substring(beginPos).replace(":",""); //.replace("\\","/"); //by wzw:keep as same as File.separator
				log.info("需要删除文件["+thisCopyTo+"]!");
				tmp_file = new File(thisCopyTo);

				if(tmp_file.exists()){
					try{
						org.apache.commons.io.FileUtils.forceDelete(tmp_file);	//.delete()
						log.info("删除文件["+thisCopyTo+"]成功!");
					
					}catch(Exception e2){
						log.info("删除文件["+thisCopyTo+"]失败!\n"+e2.getMessage() );
						str_err +=thisCopyTo+"\n";
						continue;
					}
				}else{
					str_err +=thisCopyTo+"：不存在!\n";
				}

			}
			if( "".equals(str_err) ){	// 完全成功
				JOptionPane.showMessageDialog(this,"删除文件成功!");
			}else{
				JOptionPane.showMessageDialog(this,"删除文件完成，但是有如下文件删除失败：\n"+str_err);
			}

		} else if (src==jpmi_Table_CreatePath ){	// create selected path

			int[] ti_rows = getTable().getSelectedRows();
			if (ti_rows.length<1) {
				JOptionPane.showMessageDialog(this,"请选择要创建的行！");
				return ;
			}
			 if(JOptionPane.showConfirmDialog(this,"确认执行选中的 "+ti_rows.length+"行的文件夹创建吗？","提示",JOptionPane.OK_CANCEL_OPTION)
			 				!=JOptionPane.OK_OPTION)
			 			return;

			String ts_path = ""; 
			 // FilesCopyBean.setFilePath( System.getProperty(MainFrame.USER_DATA_PATH));
			 try {		
				 File file = null;
				 for (int ti_row : ti_rows) {
					 if(ti_row<0) break;
					 ts_path = getTable().getValueAt( ti_row,1).toString() ;
					 file = new File(ts_path);
					 if (file.exists()) {
						 continue;
					 }else{
						 file.mkdirs();
						 log.info("create file path: "+ts_path + " successfully.");
					 }
				 }
				 JOptionPane.showMessageDialog(this, "文件夹创建完成！" );
			
			 } catch (Exception e1) {
				JOptionPane.showMessageDialog(this, "文件夹创建完成，但是有错误：" + e1.getMessage() );
				return;
			 }


		} else if (src==jpmi_Table_Execute_Path_Copy ){	// 复制文件全路径

			int[] ti_rows = getTable().getSelectedRows();
			for (int ti_row : ti_rows) {
				if(ti_row<0) break;
				String thisPath = getTable().getValueAt( ti_row,1).toString();
				Clipboard system = Toolkit.getDefaultToolkit().getSystemClipboard();
				StringSelection stsel = new StringSelection( thisPath );
				system.setContents(stsel,null);
			}

		} else if (src==jpmi_Table_Open ){	// Open file or path

			int[] ti_rows = getTable().getSelectedRows();
			for (int ti_row : ti_rows) {
				if(ti_row<0) break;
				String thisPath = getTable().getValueAt( ti_row,1).toString();
				DirectorFileUtils.openFileQuietly(thisPath);
			}

		}
		
	}

	
	/**
	 * implements MouseListener
	 * @param e
	 */
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		Object obj = e.getSource() ;
		if(obj==getTable()){
			// 在Table上右击鼠标
			int ti_row = getTable().getSelectedRow();
			if(ti_row<0) return;
			
			String ts_useful = getTable().getValueAt( ti_row,0).toString();

			if( e.getButton() == MouseEvent.BUTTON3 ) {	//显示弹出菜单
				
				//System.out.println (getTable().getX()+e.getX());
				//getPopupMenuForTable().locate( getTable().getX()+e.getX(), getTable().getY()+e.getY() );
				//getPopupMenuForTable().setVisible( true );
				getPopupMenuForTable().setSelected(null);
				
				if(ts_useful.equals("Y")){
					jpmi_Table_Useful.setText("设置为不启用");
				} else if(ts_useful.equals("N")){
					jpmi_Table_Useful.setText("设置为启用");
				} else {
					return ;
				}
				
				getPopupMenuForTable().show( e.getComponent(), e.getX(), e.getY() );
				
			}
		}
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"
